package by.yakusekmaxim.bookgid.webapp.page.admin.autor;

import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;

import by.yakusekmaxim.bookgid.datamodel.Author;
import by.yakusekmaxim.bookgid.datamodel.enums.PublicationStatus;
import by.yakusekmaxim.bookgid.services.AuthorService;
import by.yakusekmaxim.bookgid.webapp.page.AdminLayout;
import by.yakusekmaxim.bookgid.webapp.page.admin.image.InsertImagePanel;
import by.yakusekmaxim.bookgid.webapp.page.utils.renderer.PublicationStatusRenderer;

public class EditAuthorPage extends AdminLayout {

	@Inject
	private AuthorService authorService;

	private Author author;

	private Page pastPage;

	@Override
	protected WebMarkupContainer getTarget() {
		return targetAutor;
	}

	protected void onInitialize() {
		super.onInitialize();

		if (author.getId() == null) {
			add(new Label("pageNameLabel", new ResourceModel(
					"p.admin.editauthor.pageName.newAuthor").getObject()));
		} else {
			add(new Label("pageNameLabel", new ResourceModel(
					"p.admin.editauthor.pageName").getObject()
					+ author.getName()));
		}

		if (author.getStatus() == null) {
			PublicationStatus status = PublicationStatus.publicationstatus_edited;
			author.setStatus(status);
		}

		
		
		Form<Author> form = new Form<Author>("form",
				new CompoundPropertyModel<Author>(author));
		final Label tfId = new Label("id", author.getId());
		form.add(tfId);

		final TextField<String> tfName = new TextField<String>("name");
		tfName.add(new PropertyValidator<String>());
		form.add(tfName);

		final TextField<String> tfNameOr = new TextField<String>("nameOr");
		tfNameOr.add(new PropertyValidator<String>());
		form.add(tfNameOr);

		final DateTextField tfYearBirth = new DateTextField("yearBirth");
		tfYearBirth.add(new PropertyValidator<String>());
		form.add(tfYearBirth);

		final DateTextField tfYearDeath = new DateTextField("yearDeath");
		tfYearDeath.add(new PropertyValidator<String>());
		form.add(tfYearDeath);

		final TextField<String> tfBirthplace = new TextField<String>(
				"birthplace");
		tfBirthplace.add(new PropertyValidator<String>());
		form.add(tfBirthplace);

		final TextArea<String> tfBiography = new TextArea<String>("biography");
		tfBiography.add(new PropertyValidator<String>());
		form.add(tfBiography);

		List<PublicationStatus> choicesStatus = Arrays.asList(PublicationStatus
				.values());
		form.add(new DropDownChoice<PublicationStatus>("status", choicesStatus,
				PublicationStatusRenderer.INSTANCE));
		
		Label imageLabel = new Label("imageLabel", "");
		if (author.getImage() != null) {
			imageLabel.add(AttributeModifier.replace("src", author.getImage().getUrl()));
		} else {
			imageLabel
					.add(AttributeModifier.replace("style", "display: none;"));
		}
		imageLabel.setOutputMarkupId(true);
		form.add(imageLabel);
		
		final InsertImagePanel insertImage = new InsertImagePanel("insertImage",imageLabel ,author);
		insertImage.add(AttributeModifier.replace("style",
				"display: none;"));
		insertImage.setOutputMarkupId(true);
		add(insertImage);
		
		AjaxLink<Void> ajaxSelectPicture = new AjaxLink<Void>("selectPicture") {
			@Override
			public void onClick(AjaxRequestTarget target) {
				insertImage.add(AttributeModifier.replace("style",
						"display: block;"));
				target.add(insertImage);
			}

		};

		form.add(ajaxSelectPicture);

		form.add(new SubmitLink("cancel") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				System.out.println("!!!!!!" + pastPage);
				if (pastPage != null) {
					setResponsePage(pastPage);
				} else {
					AuthorPage page = new AuthorPage();
					setResponsePage(page);
				}
			}

			@Override
			public void onError() {

				super.onError();
			}
		});

		form.add(new SubmitLink("save") {
			@Override
			public void onSubmit() {
				super.onSubmit();

				authorService.saveOrUpdate(author);

				AuthorPage page = new AuthorPage();

				// page.error("Custom error");
				page.info("Профиль ID: " + author.getId() + " сохранён!");
				// page.warn("Custom warn");

				setResponsePage(page);
			}

			@Override
			public void onError() {

				super.onError();
			}
		});

		add(form);
		
		
	}

	public EditAuthorPage(final Author author) {

		this.author = author;

	}

	public EditAuthorPage(final Author author, Page pastPage) {

		this.author = author;
		this.pastPage = pastPage;
	}

	protected IModel<String> getPageTitle() {
		return new ResourceModel("p.admin.editauthor.pageTitle");
	}
}
